package com.ctgu.tengyun.bean.convert;

import com.ctgu.tengyun.bean.request.SysUserBaseInfo;
import com.ctgu.tengyun.bean.response.UserListResponse;
import com.ctgu.tengyun.domain.SysRole;
import com.ctgu.tengyun.domain.SysUser;
import com.ctgu.tengyun.manager.UserRoleScheduleManager;
import org.springframework.beans.BeanUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by liaoweizhi on 2019/8/13.
 */
public class UserConvert {

    public static UserListResponse toUserListResponse(Integer totalCount,
                                                      Integer pageSize,
                                                      List<SysUser> sysUserList) {
        UserListResponse response = new UserListResponse();

        response.setTotalCount(totalCount);
        response.setTotalPage((int) Math.ceil(totalCount / (double) pageSize));

        List<SysUserBaseInfo> userBaseInfoList = new ArrayList<>(sysUserList.size());
        sysUserList.forEach(item -> {
            SysRole sysRole = UserRoleScheduleManager.roleMap.get(item.getRoleId());
            item.setRoleName(null != sysRole ? sysRole.getName() : "");

            SysUserBaseInfo baseInfo = new SysUserBaseInfo();
            BeanUtils.copyProperties(item, baseInfo);
            userBaseInfoList.add(baseInfo);
        });

        response.setUserList(userBaseInfoList);
        return response;

    }
}
